package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class DeptController {


    @Autowired
    private DeptService deptService;

    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept) {
        deptService.addDept(dept);
        return Result.success();
    }


    @GetMapping("/depts")
    public Result list(String name, Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        System.out.println("列表查询:" + name + "" + status + "" + page + "" + pageSize);
        PageResult<Dept> pageResult = deptService.list(name, status, page, pageSize);
        return Result.success(pageResult);

    }

    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        System.out.println("查询id:" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        System.out.println("修改部门: " + dept);
        deptService.update(dept);
        return Result.success();
    }

    @DeleteMapping("/depts/{id}")
    public Result deleteById(@PathVariable Integer id){
        System.out.println("删除部门:" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/depts/list")
    public Result listAll(){
        return Result.success(deptService.listAll());
    }
}
